﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using 纺织贸易管理系统;
using 纺织贸易管理系统.设置窗体;

namespace 后整理管理系统.数据分析
{
    public partial class 打卷分组统计 : Form
    {
        public 打卷分组统计()
        {
            InitializeComponent();
        }

        private void 打卷分组统计_Load(object sender, EventArgs e)
        {
            dateEdit2.DateTime = DateTime.Now;
            dateEdit1.DateTime = DateTime.Now.Date;
        }

        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Query();
        }
        private void Query()
        {
            var dt = new DataTable();
            try
            {
                if (radcheck.Checked)
                {
                     dt = Connect.dbcontext.Ado.GetDataTable($"select Checkid as 打卷员, sum(biaoqianmishu) as 票签长度,sum(mishu) as 码表长度,count(*) as 合计卷数 from juanhaotable where rq between '{dateEdit1.DateTime.Date }' and '{dateEdit2.DateTime.Date.AddDays(1)}' group by Checkid");
                    chart1.Series.Clear();
                    var series1 = new System.Windows.Forms.DataVisualization.Charting.Series
                    {
                        Name = "打卷员",
                        Color = System.Drawing.Color.Blue,
                        IsValueShownAsLabel = true,
                        ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column
                    };
                    // 绑定数据
                    foreach (DataRow row in dt.Rows)
                    {
                        series1.Points.AddXY(
                            Convert.ToString(row["打卷员"]),
                            Convert.ToInt32(row["码表长度"]));
                    }
                    chart1.Series.Add(series1);
                }
                else
                {
                    dt = Connect.dbcontext.Ado.GetDataTable($"select machineid as 机台号, sum(biaoqianmishu) as 票签长度,sum(mishu) as 码表长度,count(*) as 合计卷数 from juanhaotable where rq between '{dateEdit1.DateTime.Date }' and '{dateEdit2.DateTime.Date.AddDays(1)}' group by machineid");
                    chart1.Series.Clear();
                    var series1 = new System.Windows.Forms.DataVisualization.Charting.Series
                    {
                        Name = "机台号",
                        Color = System.Drawing.Color.Blue,
                        IsValueShownAsLabel = true,
                        ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Column
                    };
                    // 绑定数据
                    foreach (DataRow row in dt.Rows)
                    {
                        series1.Points.AddXY(
                           Convert.ToString(row["机台号"]),
                            Convert.ToInt32(row["码表长度"]));
                    }
                    chart1.Series.Add(series1);
                }
                gridView1.Columns.Clear();
                gridControl1.DataSource = dt;
                decimal totalbiaoqian=0, totalmishu=0, count=0;
                for(int row=0;row<dt.Rows.Count;row++)
                {
                    totalbiaoqian += Convert.ToDecimal(dt.Rows[row]["票签长度"]);
                    totalmishu += Convert.ToDecimal(dt.Rows[row]["码表长度"]);
                    count += Convert.ToDecimal(dt.Rows[row]["合计卷数"]);
                }
                txtcount.Text = count.ToString("N0");
                txtTotalbiaoqian.Text = totalbiaoqian.ToString();
                txttotalmishu.Text = totalmishu.ToString();
               
            }
          
            catch (Exception ex)
            {
                Tools.ShowMessageBox.ShowErrorBox("生成报表的时候发生了错误！"+ ex.Message);
            }
        }

        private void 导出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ExportFile.导出到文件(gridControl1, "打卷分组汇总");
        }
    }
}
